<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="用户名称" prop="queryUserName">
        <el-input
          v-model="queryParams.queryUserName"
          placeholder="请输入用户名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="openSelectUser"
         
          >添加用户授权</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="CircleClose"
          :disabled="multiple"
          @click="cancelAuthUserAll"
       
          >批量取消授权</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          v-if="route.params.roleId == 3"
          plain
          icon="Plus"
          @click="handleAllPainter"
          v-hasPermi="['system:role:add']"
          >一键快速授权所有画师</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Close" @click="handleClose"
          >关闭</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="QQ号码"
        prop="id"
        :show-overflow-tooltip="true"
      />

      <el-table-column label="昵称" prop="nickname" :show-overflow-tooltip="true" />

      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="CircleClose"
            @click="cancelAuthUser(scope.row)"
            v-hasPermi="['system:role:remove']"
            >取消授权</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <select-user
      ref="selectRef"
      :roleId="queryParams.roleId"
      @ok="handleQuery"
    />
  </div>
</template>

<script setup name="AuthUser">
import selectUser from "./selectUser";
import {
  allocatedUserList,
  authUserCancel,
  authUserCancelAll,
} from "@/api/system/role";
import { listUser, updateUser, addUser, getUser } from "@/api/business/user";
import { listRoleUser, delRoleUser, addRoleUser } from "@/api/business/role";
import { listPainter } from "@/api/business/painter";

const route = useRoute();
const { proxy } = getCurrentInstance();

const userList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const multiple = ref(true);
const total = ref(0);
const userIds = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId,
  queryUserName: undefined,
});

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  listRoleUser(queryParams).then((response) => {
    userList.value = response.data.rows;
    total.value = response.data.count;
    loading.value = false;
  });
}
// 返回按钮
function handleClose() {
  const obj = { path: "/system/role" };
  proxy.$tab.closeOpenPage(obj);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  userIds.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}
/** 打开授权用户表弹窗 */
function openSelectUser() {
  proxy.$refs["selectRef"].show();
}
/** 取消授权按钮操作 */
function cancelAuthUser(row) {
  const roleId = queryParams.roleId;
  proxy.$modal
    .confirm('确认要取消该用户"' + row.username + '"角色吗？')
    .then(function () {
      let sendData = [{ userId: row.id, roleId: roleId }];
      return delRoleUser({ ids: sendData });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}
/** 批量取消授权按钮操作 */
function cancelAuthUserAll(row) {
  const roleId = queryParams.roleId;
  const uIds = userIds.value.join(",");
  let sendData = [];
  sendData = userIds.value.map((item) => {
    return {
      userId: item,
      roleId: roleId,
    };
  });

  proxy.$modal
    .confirm("是否取消选中用户授权数据项?")
    .then(function () {
      return delRoleUser({ ids: sendData });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}
/** 批量授权所有 */
function handleAllPainter(row) {
  listPainter().then((response) => {
    let arr = response.data;
    let sendData = [];
    sendData = arr.map((item) => {
      return {
        roleId: 3,
        userName: item.userName,
      };
    });
    addRoleUser({ ids: sendData }).then((res) => {
      proxy.$modal.msgSuccess(res.msg);
      if (res.code === 200) {
        getList();
      }
    });
  });
}

getList();
</script>
